package com.Xht.www.servlet;

//后端响应查看钱包余额的请求

import com.Xht.www.dao.UserWalletBalanceDAO;
import com.Xht.www.po.UserWalletBalancePO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

@WebServlet("/check-balance")
public class CheckBalanceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();

        int userId = Integer.parseInt(request.getParameter("userId"));
        BigDecimal roomPrice = new BigDecimal(request.getParameter("roomPrice"));

        UserWalletBalanceDAO dao = new UserWalletBalanceDAO();
        UserWalletBalancePO walletBalance = dao.getUserByUserId(userId);

        boolean success = false;
        if (walletBalance != null && walletBalance.getTotalAmount().compareTo(roomPrice) >= 0) {
            success = true;
        }

        String json = "{\"success\": " + success + "}";
        out.print(json);
        out.flush();
        out.close();
    }
}